
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#pragma once

//base class
#include "GRTParamLightPoint.h"

#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>
#include <QGroupBox>

#include "GRTPointLight.h"
#include "GRTCommonParameter.h"
#include "GRTScene.h"
#include "GRTMemoryLeak.h"
//constructor
GRTParamLightPoint::GRTParamLightPoint(QWidget* parent) : GRTBaseParamWidget(parent)
{
	//Title
	_title = new QLabel("POINT LIGHT");

	//constant attenuation
	_labelConstantAttenuation = new QLabel("Constant : ");
	_constantAttenuationLineEdit = new QLineEdit();

	//linear attenuation
	_labelLinearAttenuation = new QLabel("Linear : ");
	_linearAttenuationLineEdit = new QLineEdit();

	//quadratic attenuation
	_labelQuadraticAttenuation = new QLabel("Quadratic : ");
	_quadraticAttenuationLineEdit = new QLineEdit();

	QGridLayout* attenuationLayout = new QGridLayout();
	attenuationLayout->addWidget(_labelConstantAttenuation, 0, 0, 1, 1);
	attenuationLayout->addWidget(_constantAttenuationLineEdit, 0, 1, 1, 1);
	attenuationLayout->addWidget(_labelLinearAttenuation, 1, 0, 1, 1);
	attenuationLayout->addWidget(_linearAttenuationLineEdit, 1, 1, 1, 1);
	attenuationLayout->addWidget(_labelQuadraticAttenuation, 2, 0, 1, 1);
	attenuationLayout->addWidget(_quadraticAttenuationLineEdit, 2, 1, 1, 1);
	QGroupBox* groupAttenuation = new QGroupBox("Attenuation");
	groupAttenuation->setLayout(attenuationLayout);

	//widget's layout
	_layout = new QGridLayout();

	_layout->addWidget(_title, 0, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(_common, 1, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(groupAttenuation, 2, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(new QWidget(), 3, 0, 1, 2);
	_layout->setRowStretch(3, 1);

	setLayout(_layout);

	//connect the attenuation
	connect(_constantAttenuationLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setAttenuation()));
	connect(_linearAttenuationLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setAttenuation()));
	connect(_quadraticAttenuationLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setAttenuation()));

	_common->_groupRotation->hide();
	_common->_groupScale->hide();
	_common->_layout->setMargin(0);
}

//destructor
GRTParamLightPoint::~GRTParamLightPoint(){}

//fill all the widgets with values
void GRTParamLightPoint::fillWidgets(GRTILight* l)
{
	_common->fillWidgets(l);

	GRTPointLight* p = (GRTPointLight*)l;

	_constantAttenuationLineEdit->setText(QString::number(p->getAttenuation().x));
	_linearAttenuationLineEdit->setText(QString::number(p->getAttenuation().y));
	_quadraticAttenuationLineEdit->setText(QString::number(p->getAttenuation().z));
}



//set the primitive dimensions
void GRTParamLightPoint::setAttenuation()
{
	//get the selected object
	GRTMovable* m = SCENE.getSelectedObject();

	//if no object selected
	if(m == 0)
		return;

	//if the selected object is not a light
	if(m->getGRTMovableType() != MT_LIGHT)
		return;

	//if the light is not a point light
	GRTILight* l = (GRTILight*)m;
	if(l->getType() != LT_POINT)
		return;

	//set the attenuation
	GRTPointLight* p = (GRTPointLight*)l;
	p->setAttenuation(RTVector3f(_constantAttenuationLineEdit->text().toFloat(),
		_linearAttenuationLineEdit->text().toFloat(),
		_quadraticAttenuationLineEdit->text().toFloat()));
	
}
